import { requestApi } from '../../api/request'
const app = getApp()
Page({
    data: {
        preloading:true,
        param:{
            action:'search',
            keyword:'',
            page:1
        },
        searchbl:false,
        searchResult: true
    },
    onInit(option){
        this.data.param.keyword = app.data.keyword
        let param = this.data.param
        this.setData({
            keyword:app.data.keyword
        })
        // console.log(param)
        // console.log(app.data.keyword)
        this.getdata(param)
    },
    onLoad: function () {
        // 监听页面加载的生命周期函数
    },
    onReady: function() {
        // 监听页面初次渲染完成的生命周期函数
    },
    onShow: function() {
        // 监听页面显示的生命周期函数
    },
    onHide: function() {
        // 监听页面隐藏的生命周期函数
    },
    onUnload: function() {
        // 监听页面卸载的生命周期函数
    },
    onPullDownRefresh: function() {
        // 监听用户下拉动作
    },
    onReachBottom: function() {
        // 页面上拉触底事件的处理函数
    },
    onShareAppMessage: function () {
        // 用户点击右上角转发
    },
    onReachBottom: function() {
        // 上拉触底
        let page = this.data.param.page
        page++
        this.data.param.page = page
        let param = this.data.param
        console.log(param)
        requestApi(param).then(
            res => {
                // console.log(res)
                let newslistitem = res.data.list
                // console.log(newslistitem)
                let newslist = this.data.newslist.concat(newslistitem)
                // console.log(newslist)
                this.setData({
                    newslist
                })
            }
        )
    },
    getdata(param){
        requestApi(param).then(
            res => {
                console.log(res.data)
                let newslist = res.data ? res.data.list : ''
                if(res.data !== null){
                    this.setData({
                        searchResult:false
                    })
                    console.log(this.data.searchResult)
                }
                this.setData({
                    preloading:false,
                    newslist,
                })
            }
        )
    },
    bindKeyInput(e){
        app.data.keyword = e.detail.value
    },
    searchtap(){
        console.log(app.data.keyword)
        let keyword = app.data.keyword
        swan.navigateTo({
            url: "/pages/search/search?keyword=" + keyword
        });
    }
});